<template>
    <!-- 添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :before-close="beforeClose" :title="title" :visible="visible"
               append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <#list  columns as field>
                <#if field_index %2==0>
                    <el-row>
                    <el-col :span="12" class="grid-cell">
                        <el-form-item label="${field.columnComment!}" prop="${field.columnName!}">2
                            <el-input v-model="${field.columnName!}" placeholder="请输入${field.columnComment!}"/>
                        </el-form-item>
                    </el-col>
                    <#if !field_has_next>
                        </el-row>
                    </#if>
                <#else>
                    <el-col :span="12" class="grid-cell">
                        <el-form-item label="${field.columnComment!}" prop="${field.columnName!}">2
                            <el-input v-model="${field.columnName!}" placeholder="请输入${field.columnComment!}"/>
                        </el-form-item>
                    </el-col>
                    </el-row>
                </#if>
            </#list>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="close">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {getAction, jsonAction, postAction} from "@/api/action";

    export default {
        name: "${TableName!}Dialog",
        props: {
            title: {
                type: String,
                default: ""
            },
            visible: {
                type: Boolean,
                default: false,
            },
            infoId: {
                type: [String, Number],
                default: "",
                required: true
            }
        },
        data() {
            return {
                form: {},
                rules: {},
                url: {
                    add: "/${tableName!}/baseAdd",
                    update: "/${tableName!}/baseUpdate"
                }
            }
        },
        created() {
        },
        methods: {
            //预留的初始化函数,详情拿回来后会调用这里
            initData(data) {

            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            jsonAction(this.url.update, this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                                this.close()
                            });
                        } else {
                            jsonAction(this.url.add, this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                                this.close()
                            });
                        }
                    }
                });
            },
// 表单重置
            reset() {
                this.form = {
                    name: null,
                    code: null,
                };
                this.$refs["form"].resetFields();
            },
            close() {
                this.reset();
                this.$emit('update:visible', false)
                //告诉外面弹框关掉了，用于让外部的列表刷新一下
                this.$emit('close')
            },
            beforeClose() {
                this.close()
            }
        },
        watch: {
            infoId(id) {
                if (id) {
                    getAction("/${tableName!}/baseGetById", {id: this.infoId}).then(response => {
                        this.form = response.data;
                    });
                }

            }
        }
    }
</script>

<style scoped>

</style>
